#include "analy/signal/SignalTool.h"

SignalTool::SignalTool(QMap<QString, QMap<QString, QString>> &_RGLRLSMAP_,
                       QSqlDatabase *DB,
                       QMap<QString, QString> &_QSSMAP_)
    :ToolLogicBT(_RGLRLSMAP_, DB, _QSSMAP_)
{
    //#============================QTimer
    GLOBAL_DBLOOP_QTIER.setInterval(10000);
    GLOBAL_DBLOOP_QTIER.start();
    SRLP_LOOPGETP_QTIER.setInterval(4000);
    SRLP_HLDLECHO_QTIER.setInterval(1000);
    SRLP_HLDLECHO_QTIER.setSingleShot(true);
    SRLP_DLRMV_QTIER.setInterval(200);
    SRLP_DLRMV_QTIER.setSingleShot(true);

    GLOBAL_ARGUR_QTIER.setInterval(10);
    GLOBAL_ARGUR_QTIER.setSingleShot(true);

    SRLP_HLDLTRIGG_QTIER.setInterval(1000);
    SRLP_HLDLTRIGG_QTIER.setSingleShot(true);

    //#==========QMenu
    connect(TR_actionMenu, &QMenu::triggered, this, &SignalTool::roofmenu_option);
    connect(FL_actionMenu, &QMenu::triggered, this, &SignalTool::roofmenu_option);
    connect(AS_actionMenu, &QMenu::triggered, this, &SignalTool::roofmenu_option);
    connect(SP_configMenu, &QMenu::triggered, this, &SignalTool::roofmenu_option);
    connect(VW_actionMenu, &QMenu::triggered, this, &SignalTool::abstract_function_mchoice);
    connect(SP_usbPortMenu, &QMenu::triggered, this, [this](QAction *AC){data_serial_detailconfig(AC->text(), SerialPort, true);});
    connect(SP_baudRateMenu, &QMenu::triggered, this, [this](QAction *AC){data_serial_detailconfig(AC->text(), BaudRate, true);});
    connect(SP_dtplaceMenu, &QMenu::triggered, this, [this](QAction *AC){data_serial_detailconfig(AC->text(), DtPlace, true);});
    connect(SP_stplaceMenu, &QMenu::triggered, this, [this](QAction *AC){data_serial_detailconfig(AC->text(), StPlace, true);});
    connect(SP_ckplaceMenu, &QMenu::triggered, this, [this](QAction *AC){data_serial_detailconfig(AC->text(), CheckPlace, true);});
    connect(SP_scontrolMenu, &QMenu::triggered, this, [this](QAction *AC){data_serial_detailconfig(AC->text(), Scontrol, true);});

    //#==========QPushButton
    connect(VW_viewcutBut, &QPushButton::clicked, this, &SignalTool::abstract_function_cchoice);
    connect(QR_saveBut, &QPushButton::clicked, this, [this](){data_qrcodebranch(NT::CUT_QRCODESAVE);});
    connect(this, &SignalTool::bsic_quit, this, &SignalTool::abstract_systemexit);
    connect(this, &SignalTool::bsic_nrrls, this, &SignalTool::abstract_dtbs_timer);
    connect(DT_exec, &NinsipidView::msev_rls, this, &SignalTool::data_execbut_clickev);
    connect(DT_remove, &NinsipidView::msnm_rls, this, &SignalTool::data_list_remove);
    connect(DT_scview, &NinsipidView::msnm_rls, this, [this](QString name){data_txtboxasb_facecut(name);});
    connect(DT_rsview, &NinsipidView::msnm_rls, this, [this](QString name){data_txtboxasb_facecut(name);});
    connect(DT_hlview, &NinsipidView::msnm_rls, this, [this](QString name){data_txtboxasb_facecut(name);});
    connect(SP_ksubmitBut, &QPushButton::pressed, this, &SignalTool::data_serial_kwfind);

    connect(SP_hlsubmitBut, &QPushButton::clicked, this, &SignalTool::data_serial_hlbranch);
    connect(SP_connectSqlAlterBut, &QPushButton::clicked, this, [this](){data_serial_connectbranch(NT::CUT_SERIALUPDATE);});
    connect(SP_commandsendBut, &QPushButton::clicked, this, [this](){data_serial_commandbranch(NT::CUT_COMMANDSEND);});
    connect(SP_commandsaveBut, &QPushButton::clicked, this, [this](){data_serial_commandbranch(NT::CUT_COMMANDSAVE, SP_commandEdit->toPlainText());});
    connect(SP_commandinputBut, &QPushButton::clicked, this, &SignalTool::data_serial_commandimport);
    connect(SP_connectSqlSaveBut, &QPushButton::clicked, this, [this](){data_serial_connectbranch(NT::CUT_SERIALSAVE);});

    //#==========QListWidget
    connect(BS_listWidget, &QListWidget::doubleClicked, this, [this](const QModelIndex &index){data_listitem_dbclickev(index.row());});
    connect(BS_listWidget, &QListWidget::itemSelectionChanged, this, &SignalTool::data_listitem_slctev);
    connect(BS_listWidget, &QListWidget::currentRowChanged, this, &SignalTool::data_tool_execstart);
    connect(BS_listWidget, &QListWidget::itemClicked, this, &SignalTool::data_listitem_slctev);

    //#==========QTabWidget
    connect(SP_TabWidget, &QTabWidget::currentChanged, this, [this](int index){data_serial_hlrecover(index);});

    //#==========DiaPool
    connect(BoilPool->data_paste, &StickTData::st_value, this, &SignalTool::data_scfocus_follow);
    connect(BoilPool->data_paste, &StickTData::process, this, &SignalTool::data_prograss_step);
    connect(BoilPool->data_paste, &StickTData::finish, this, &SignalTool::data_execbut_stat);

    connect(SP_hlbkclLabel, &NinsipidView::msev_rls, this, [this](){data_serial_hlclrcfg(false);});
    connect(SP_hlfrclLabel, &NinsipidView::msev_rls, this, [this](){data_serial_hlclrcfg(true);});
    connect(DT_scdtEditAsClear, &NinsipidView::msnm_rls, this, [this](QString name){data_txtboxasb_triggered(NT::CUT_DEPUTYCLEAR, name);});
    connect(DT_rsdtEditAsClear, &NinsipidView::msnm_rls, this, [this](QString name){data_txtboxasb_triggered(NT::CUT_DEPUTYCLEAR, name);});
    connect(SP_serialEditAsDownLoad, &NinsipidView::msnm_rls, this, [this](QString name){data_txtboxasb_triggered(NT::CUT_DEPUTYDOWNLOAD, name);});
    connect(SP_serialEditAsClear, &NinsipidView::msnm_rls, this, [this](QString name){data_txtboxasb_triggered(NT::CUT_DEPUTYCLEAR, name);});
    connect(SP_serialEditAsUpdater, &NinsipidView::msnm_rls, this, [this](QString name){data_txtboxasb_triggered(NT::CUT_DEPUTYUPDATE, name);});

    //#==========QSerialPort
    connect(SP_sralPort, &QSerialPort::readyRead, this, &SignalTool::data_txtbox_focusfollow);
    connect(SP_sralPort, &QSerialPort::errorOccurred, this, &SignalTool::data_serial_error);
    connect(SP_sralPort, &QSerialPort::baudRateChanged, this, &SignalTool::data_listitem_unslctev);
    connect(SP_sralPort, &QSerialPort::stopBitsChanged, this, &SignalTool::data_listitem_unslctev);
    connect(SP_sralPort, &QSerialPort::dataBitsChanged, this, &SignalTool::data_listitem_unslctev);
    connect(SP_sralPort, &QSerialPort::flowControlChanged, this, &SignalTool::data_listitem_unslctev);
    connect(SP_sralPort, &QSerialPort::parityChanged, this, &SignalTool::data_listitem_unslctev);

    //#==========QEdit
    connect(SP_commandEdit, &NPlainEdit::KeyEvent, this, [this](QPair<int, QString> ifo){if (ifo.second == "\r") data_serial_commandbranch(NT::CUT_COMMANDSEND);});
    connect(SP_ksearchEdit, &QLineEdit::textChanged, this, [this](QString text){data_serial_hlrecover(text);});
    connect(SP_serialInfoEdit->verticalScrollBar(), &QScrollBar::valueChanged, this, &SignalTool::data_serial_hltriggered);
    connect(SP_serialInfoEdit, &NPlainEdit::pixmap_empty_error, this, &SignalTool::data_assistbut_error);
    connect(DT_scdtEdit, &NPlainEdit::pixmap_empty_error, this, &SignalTool::data_assistbut_error);
    connect(DT_rsdtEdit, &NPlainEdit::pixmap_empty_error, this, &SignalTool::data_assistbut_error);

    //#==========NMatchKeyWord / HighLight
    connect(MatchKeyWord, &NMatchKeyWord::cutsor_send, this, &SignalTool::data_txtbox_passivityfollow);
    connect(SyntaxHLight, &NMatchKeyWord::taskMatchtrue, this, &SignalTool::data_task_runend);

    //#==========QCheckBox
    connect(SP_sensiticeCheck, &QCheckBox::stateChanged, this, [this](int state){data_serial_kwoption(1, state);});
    connect(SP_findBackward, &QCheckBox::stateChanged, this, [this](int state){data_serial_kwoption(0, state);;});
    connect(QR_modeCutBox, &QCheckBox::stateChanged, this, [this](int state){(state==2) ? QR_keysEdit->setVisible(true) : QR_keysEdit->setVisible(false);});
    connect(QR_saveQrBox, &QCheckBox::stateChanged, this, [this](int state){if (state==2) {data_qrcodebranch(NT::CUT_QRCODEPROD);data_qrcodebranch(NT::CUT_QRCODESAVE);}});

    //#==========QTimer / Local Signals
    connect(&GLOBAL_DBLOOP_QTIER, &QTimer::timeout, this, &SignalTool::abstract_dtbs_timer);
    connect(&TOOL_MENUADJST_QTIER, &QTimer::timeout, this, &SignalTool::MenuWidthIntilize);
    connect(&SRLP_DLRMV_QTIER, &QTimer::timeout, this, &ToolLogicCT::data_listitem_echo);
    connect(&SRLP_LOOPGETP_QTIER, &QTimer::timeout, this, [this](){data_serialport_loopc();});
    connect(&SRLP_HLDLECHO_QTIER, &QTimer::timeout, this,[this](){data_reserve_loader(NT::CUT_HLITEXTLIST, false);});
    connect(&SRLP_HLDLTRIGG_QTIER, &QTimer::timeout, this, &SignalTool::data_serial_hlcfgexec);
    connect(&SRLP_CMDLOOP_QTIER, &QTimer::timeout, this, [&](){data_task_runend(impact_str["TaskForReboot"]);});

    connect(&GLOBAL_ARGUR_QTIER, &QTimer::timeout, this, &SignalTool::basic_configration_init);
    connect(this, &SignalTool::VariantPopuEvent, this, &SignalTool::VariantTaskDispose);
}

void SignalTool::MenuWidthIntilize(){
    SP_setActionAdjustSize(TR_actionBut);
    SP_setActionAdjustSize(FL_fileaddBut);
    SP_setActionAdjustSize(AS_assistBut);
    SP_setActionAdjustSize(SP_serialBut);
    SP_setActionAdjustSize(VW_viewcutBut);
    SP_setActionAdjustSize(SP_usbportBut);
    SP_setActionAdjustSize(SP_baudrateBut);
    SP_setActionAdjustSize(SP_dtplaceBut);
    SP_setActionAdjustSize(SP_stplaceBut);
    SP_setActionAdjustSize(SP_ckplaceBut);
    SP_setActionAdjustSize(SP_scontrolBut);
}

void SignalTool::VariantTaskDispose(QPair<NT::TASKEXEC, QVariant> task){
    if (task.first == NT::ANALYFUNC_BACK_COMPARE)
    {
        data_tool_execstart(task.second.toUInt());
    }
}
